package com.zhijiaoyun.task;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * @author shisfish
 * @date 2018/1/23
 */
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {

	private Log logger = LogFactory.getLog(WebsocketEndPoint.class);

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Map<String, Object> attributes) throws Exception {
		logger.debug("GOMA ===> Before Handshake");

		// if (request instanceof ServletServerHttpRequest) {
		// ServletServerHttpRequest servletRequest = (ServletServerHttpRequest)
		// request;
		// HttpSession session =
		// servletRequest.getServletRequest().getSession(false);
		// if (session != null) {
		// //使用userName区分WebSocketHandler，以便定向发送消息
		// String userName = (String)
		// session.getAttribute(Constants.SESSION_USERNAME);
		// attributes.put(Constants.WEBSOCKET_USERNAME,userName);
		// }
		// }

		return super.beforeHandshake(request, response, wsHandler, attributes);
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception ex) {
		logger.debug("GOMA ===> After Handshake");
		super.afterHandshake(request, response, wsHandler, ex);
	}
}
